$(function(){
	//ajax全局设置
	$.ajaxSetup({
		 beforeSend: function(request) {
            request.setRequestHeader("X-AIRPORT-TOKEN", localStorage.getItem("oauth_token"));
        }
	})
	var specialList=function(){$.ajax({
		type:'get',
		url:baseURL+"/api/special/list?iDisplayStart="+0+"&iDisplayLength="+10+"&refever="+Math.random(),
		dataType:"JSON",
		contentType: "application/json",
		success:function(result){
		   if(!result.flag){
	    		   layer.msg("数据错误原因："+result.message);
	    		   return;
	    	   }
    	   if(result.flag){
    		   if(result.results.length!=0){
	    		   $.each(result.results,function(i,n){
	    			   $('#specialShowList').find('tbody').append("<tr onclick=\"specialSetURL('"+n.ssid+"','"+n.publishId+"')\"><td>"+n.level+"</td><td>"+n.topic+"</td><td>"+getTime(n.publishTime)+"</td><td>"+getStatus(n.status,n.sStatus)+"</td></tr>")
	    		   })
    		   }else{
    			   $('#specialShowList').find('tbody').html("<tr class='text-c'><td colspan='4'>没有数据！</td></tr>")
    		   }
    	   }
		}
	})
	}
	specialList();
	
	//ILINK回调
    var isNeedRelogin=true;
	function onClientEvent(event){
	    if(event.type === 'message'){
	        if(event.topic == 'chat'){
	        	var msg = $.parseJSON(event.message);
	        	if(msg.msg_type=="SEPCIAL"){
	        		$("#specialShowList").find('tbody').empty();
	        		setTimeout(specialList(),5000);
	        	}
	        }else if(event.topic == 'sys' && event.from_userid === 'kick'){
	            console.log('receive ' +  event.topic + ' message from ' + event.from_userid + ' : ' + event.message);
	            isNeedRelogin = false;
	            $.layer({
	            	  shade: [0],
	            	  area: ['auto','auto'],
	            	  dialog: {
	            	    msg: '亲，您已被踢下线！',
	            	    btns: 2,          
	            	    type: 4,
	            	    btn: ['重连','关闭'],
	            	    yes: function(){
	            	    	layer.msg("正在重连中...",1,6);
	            	    	reConnect();
	            	    }, no: function(){
	            	    	layer.msg('关闭成功',1,1)
	            	    	CloseWebPage();
	            	    }
	            	  }
	            	});
	        }
		 }else if (event.type == 'connect') {
	            //登录成功
	            console.log("登录IM成功");
	        } else if (event.type === 'reconnect') {
	            console.log('重新登录');
	        } else if (event.type === 'offline') {
	            console.log('已下线');
	        } else if (event.type === 'close') {
	            console.log('连接关闭,注销成功!');
	            if(isNeedRelogin){
	            	console.log("亲，您掉线了哦，请退出我的设备的控制界面，重新进入");
	            	reConnect();
	            }
	        } else if (event.type === 'error') {
	            console.log("用户名或密码错误! " + event.error.message + '(' + event.error.code + ')');
	            console.log(event.error.message + '(' + event.error.code + ')');
	            layer.msg("IM连接失败！",1,1);
	        }
	}
	//方法签名
	var config = {
	        appkey: appkey,
	        secretkey :skey,
	        im_host: im_host,
	        im_port:im_port,
			clean: 1,
			userid: userName,
			username:userName,
			token:localStorage.getItem("oauth_token")
	}
	//初始化环境
    var etCloud = new et_im();
	etCloud.etInitConfig(config);
	etCloud.etConnect(function(e) {
		  onClientEvent(e);
	    });
	
	function reConnect(){
	    var intervalId = setInterval(function(){
	        if(etCloud.checkIsConnected()){
	        	console.log(etCloud.checkIsConnected())
	            //已登录不用再重登
	            clearInterval(intervalId);
	        }else{
	        	etCloud.etConnect (function(e) {
	      		  onClientEvent(e);
	    	    });
	            layer.msg("您已重新连接成功！",1,1);
	        }
	    },3000);
	}
});
function getTime(t){
		 var date=new Date(t);
		 return changeTime(date.getHours())+":"+changeTime(date.getMinutes());
}
function changeTime(obj){
	return obj<10?"0"+obj:obj;
}
function getStatus(obj,obj2){
	 if(obj2=="12" || obj2=="11" || obj2=="10"){//1：接收者；2：转发者；3：自发起，可强制结束或撤销；4：已完成
		 return "<span class='c-999'>已完成</span>";
	 }
	 else if(obj=="1" || obj=="2"){//1：接收者；2：转发者；3：自发起，可强制结束或撤销；4：已完成
		 	return "<span class='c-black'>处理中</span>";
		 }else if(obj=="0"){
			 return "<span class='c-red'>待处理</span>";
		 }else return '/'
}
function specialSetURL(ssid,publishId){
	 localStorage.setItem('reloadFlag',false)
	 window.open(baseURL+"/api/special/main?showList="+ssid+"&senderId="+publishId+"&ouath_token="+localStorage.getItem("oauth_token"));
}
var specialAddShow=function(){
	localStorage.setItem('reloadFlag',false)
	window.open(baseURL+"/api/special/main?ouath_token="+localStorage.getItem("oauth_token"));
}